from pydantic import BaseModel, Field
from typing import Dict, List, Optional, Any

class ErrorResponse(BaseModel):
    """错误响应模型"""
    detail: str

class MerchantBaseInfo(BaseModel):
    """商家基本信息"""
    merchant_id: str = Field(..., description="商家ID")
    merchant_name: str = Field(..., description="商家名称")
    business_type: str = Field(..., description="经营类型")
    register_time: str = Field(..., description="注册时间")
    location: str = Field(..., description="所在地区")

class FinancialMetrics(BaseModel):
    """财务指标"""
    monthly_revenue: float = Field(..., description="月营收(元)")
    yearly_revenue: Optional[float] = Field(None, description="年营收(元)")
    profit_margin: Optional[float] = Field(None, description="利润率(%)")
    debt_ratio: Optional[float] = Field(None, description="负债率(%)")
    cash_flow: Optional[float] = Field(None, description="现金流(元)")

class OperationalMetrics(BaseModel):
    """运营指标"""
    customer_count: int = Field(..., description="客户数量")
    order_count: int = Field(..., description="订单数量")
    average_order_value: float = Field(..., description="平均订单价值(元)")
    repeat_purchase_rate: Optional[float] = Field(None, description="复购率(%)")
    customer_satisfaction: Optional[float] = Field(None, description="客户满意度(1-5)")

class RiskMetrics(BaseModel):
    """风险指标"""
    payment_delay_rate: Optional[float] = Field(None, description="付款延迟率(%)")
    chargeback_rate: Optional[float] = Field(None, description="退单率(%)")
    complaint_count: Optional[int] = Field(None, description="投诉次数")
    legal_issues: Optional[List[str]] = Field(None, description="法律问题记录")
    credit_score: Optional[int] = Field(None, description="信用评分(1-100)")

class MerchantEvaluationRequest(BaseModel):
    """商家评估请求"""
    basic_info: MerchantBaseInfo
    financial_metrics: FinancialMetrics
    operational_metrics: OperationalMetrics
    risk_metrics: Optional[RiskMetrics] = None
    additional_data: Optional[Dict[str, Any]] = Field(None, description="其他补充数据")

class ScoreResult(BaseModel):
    """评分结果"""
    score: float = Field(..., description="评分(1-100)")
    level: str = Field(..., description="等级(A+,A,B,C,D)")
    analysis: str = Field(..., description="分析说明")

class MerchantEvaluationResponse(BaseModel):
    """商家评估响应"""
    merchant_id: str
    merchant_name: str
    financial_score: ScoreResult
    operational_score: ScoreResult
    risk_score: ScoreResult
    overall_score: ScoreResult
    evaluation_time: str
    recommendations: List[str]

class MerchantDecisionRequest(BaseModel):
    """商家决策请求"""
    evaluation_result: MerchantEvaluationResponse
    decision_type: str = Field(..., description="决策类型，如'贷款'、'合作'等")
    decision_context: Optional[Dict[str, Any]] = Field(None, description="决策上下文信息")

class DecisionResult(BaseModel):
    """决策结果"""
    decision: str = Field(..., description="决策结果，如'通过'、'拒绝'等")
    confidence: float = Field(..., description="置信度(0-1)")
    reasons: List[str] = Field(..., description="决策理由")
    suggestions: Optional[List[str]] = Field(None, description="建议措施")

class MerchantDecisionResponse(BaseModel):
    """商家决策响应"""
    merchant_id: str
    merchant_name: str
    decision_result: DecisionResult
    decision_time: str 